package com.example.lenovo.demoa;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private WebView my_webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_webview = findViewById(R.id.my_webview);
        //第一种 加载网址
        my_webview.loadUrl("http://www.baidu.com");

        // 第二种 加载assets目录下的本地网页
//        my_webview.loadUrl("file:///android_asset/a.html");

        //第三种 加载html文本内容
//        String htmlContent = "<html>\n" +
//                "<title>\n" +
//                "本地网页\n" +
//                "</title>\n" +
//                "<body>\n" +
//                "hello world!\n" +
//                "<img src=\"file:///android_asset/a.jpg\">\n" +
//                "</body>\n" +
//                "</html>";

//        my_webview.loadData(htmlContent,"text/html","utf-8");
//        my_webview.loadDataWithBaseURL(null,htmlContent,
//                "text/html","utf-8",null);

        //第四种 加载SD卡上的html
//        my_webview.loadUrl("file:///mnt/shared/Other/a.html");

        //问题1 跳转的时候打开了系统浏览器
        my_webview.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view,String url){
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("MainActivity","onPageStarted");
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e("MainActivity","onPageFinished");
                super.onPageFinished(view, url);
            }
        });

        findViewById(R.id.go_forward).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (my_webview.canGoForward()) {
                    my_webview.goForward();
                }
            }
        });
        WebSettings webSettings = my_webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setDisplayZoomControls(true);
        webSettings.setBuiltInZoomControls(true);
    }
    //问题2 点击返回键推出程序
    public void onBackPressed() {
        Log.e("MainActivity", "onBackPressed");
        if (my_webview.canGoBack()) {
            my_webview.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
